package astudy.networking.tcp;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.commons.io.IOUtils;

public class FileSender extends ServerSocket {

	private File file;
	public FileSender(File file) throws IOException {
		
		super(0);
		this.file = file;
	}
	
	public void send() {
		
		try {
			Socket client = this.accept();

			FileInputStream fis = new FileInputStream(file);
			OutputStream os = new BufferedOutputStream(client.getOutputStream());
			IOUtils.copy(fis, os);
			os.close();
			fis.close();
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
